from django.db import models


class Department(models.Model):  # 部门表
    name = models.CharField('部门', max_length=64, unique=True)


class Position(models.Model):  # 机器存放位置
    name = models.CharField('机房', max_length=32)
    floor = models.IntegerField('楼层', default=1)


class Host(models.Model):  # 主机表
    hostname = models.CharField(verbose_name='主机', max_length=64)  # ip

    status_choice = (
        (1, '上线'),
        (2, '下线'),
    )
    status = models.IntegerField(verbose_name='状态', choices=status_choice, default=1)

    department = models.ForeignKey(verbose_name="所属部门", to='Department', blank=True, null=True,
                                   on_delete=models.SET_NULL)

    position = models.ForeignKey(verbose_name='机房', to='Position', blank=True, null=True, on_delete=models.SET_NULL)
    cabinet_num = models.CharField('机柜号', max_length=32, null=True, blank=True)
    cabinet_order = models.CharField('机柜中序号', max_length=32, null=True, blank=True)

    os_platform = models.CharField('系统', max_length=32, null=True, blank=True)
    os_version = models.CharField('系统版本', max_length=32, null=True, blank=True)

    sn = models.CharField('SN号', max_length=64, db_index=True, null=True, blank=True)
    manufacturer = models.CharField(verbose_name='制造商', max_length=64, null=True, blank=True)
    model = models.CharField('型号', max_length=64, null=True, blank=True)

    cpu_count = models.IntegerField('CPU个数', null=True, blank=True)
    cpu_physical_count = models.IntegerField('CPU物理个数', null=True, blank=True)
    cpu_model = models.CharField('CPU型号', max_length=128, null=True, blank=True)


class Disk(models.Model):  # 硬盘表
    slot = models.CharField(verbose_name='槽位', max_length=32)
    pd_type = models.CharField(verbose_name='类型', max_length=32)
    capacity = models.CharField(verbose_name='容量', max_length=32)
    model = models.CharField(verbose_name='型号', max_length=32)
    host = models.ForeignKey(verbose_name='服务器', to='Host', null=True, blank=True, on_delete=models.SET_NULL)


class ChangeRecord(models.Model):  # 资产变更记录表, 一条记录为某个主机中某个硬件的变更
    text = models.TextField(verbose_name='内容')
    host = models.ForeignKey(verbose_name='主机', to='Host', null=True, blank=True, on_delete=models.SET_NULL)
    time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


class Memory(models.Model):  # 内存
    host = models.ForeignKey('Host', on_delete=models.SET_NULL, null=True, blank=True)
    slot = models.CharField('插槽位', max_length=32)
    manufacturer = models.CharField('制造商', max_length=32, null=True, blank=True)
    model = models.CharField('型号', max_length=64)
    capacity = models.FloatField('容量', null=True, blank=True)
    sn = models.CharField('内存SN号', max_length=64, null=True, blank=True)
    speed = models.CharField('速度', max_length=16, null=True, blank=True)


class NetWorkCard(models.Model):  # 网卡
    name = models.CharField('网卡名称', max_length=128)
    mac = models.CharField('网卡mac地址', max_length=64)
    netmask = models.CharField(max_length=64)
    ipaddr = models.CharField('ip地址', max_length=256)
    up = models.BooleanField(default=False)
    host = models.ForeignKey('Host', on_delete=models.SET_NULL, null=True, blank=True)


class UserInfo(models.Model):
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=64)
    email = models.CharField(max_length=32)
